<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!--layui css-->
    <link rel="stylesheet" href="https://heerey525.github.io/layui-v2.4.3/layui-v2.4.5/css/layui.css">

    <style>
        .table-center{
            text-align: center;
        }
        .layui-table th{
            text-align: center;
        }
    </style>



</head>
<body>
<div class="layui-container">
    <div class="layui-row">
        <form action="#" method="post" id="form">
            <table lay-even class="layui-table table-center">
                <colgroup>
                    <col width="150">
                    <col width="200">
                    <col>
                </colgroup>
                <thead>
                <tr>
                    <th><a href="#" onclick="toSubmit()">删除</a></th>
                    <th>车次</th>
                    <th>发车——到达</th>
                    <th>发时——到时</th>
                    <th>车型</th>
                    <th>运行时间（小时）</th>
                </tr>
                </thead>
                <tbody>
                <tr class="table-center" th:if="${allTrain != null}" th:each="train : ${allTrain}">
                    <td><input type="checkbox" name="del" lay-skin="primary" th:value="${train.getTrainNo()}"></td>
                    <td><a th:href="@{'/train/'+${train.getTrainNo()}}" th:text="${train.getTrainNo()}"></a></td>
                    <td>[[${train.getStartStation()}]]——[[${train.getArrivalStation()}]]</td>
                    <td>[[${train.getStartTime()}]]——[[${train.getArrivalTime()}]]</td>
                    <td th:text="${train.getType()}"></td>
                    <td th:text="${train.runtime}"></td>
                </tr>
                </tbody>
            </table>
        </form>

    </div>
</div>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<!--layui js-->
<script  src="https://heerey525.github.io/layui-v2.4.3/layui-v2.4.5/layui.js"></script>

<script>
   // $("tr:odd").css("background-color", "#CC0000"); //隔行变色
   function toSubmit(){
      var ids=[];
      //循环所有选中的值
    var checkedBox=$("[name=del]:checked");
     if(checkedBox.length>0){
         $("[name=del]:checked").each(function (i) {
            ids[i]= $(this).val();
         });
         $.ajax({
             url:"/train/del",
             method:"post",
             dataType:"text",
             data:{
                 'ids':ids
             },
             traditional:true,
             success:function (data) {
                alert(data);
                window.location="/";
             },
             error:function (data) {
                console.log(data);
                alert("操作失败！")
                 window.location="/";
             }
         })
     }else{
        alert("请选择车次！")
     }
    }
</script>

</body>
</html>